

* Open log
capture log close
log using "Data analysis\cbld-management03-exp02.log", replace text


* *****************************************************************************
* Processing of results of computational experiment 2 for further data analysis
* *****************************************************************************

* Programme:	cbld-management03-exp02.do
* Project:		Council coalition building
* Author:		Frank Haege, Department of Politics and Administration, University of Limerick
* Contact:		frank.haege@ul.ie

* Description
*************
* This do-file uses the simulation results of the second computational experiment
* to generate data sets of predicted consensus rates and consensus rates expected by chance. 
* The do-file also demonstrates how the 'typical' case illustrated in the article (Figure 3) 
* was identified.


* Set up Stata
version 11
clear all
macro drop _all
set linesize 80
set more off

* Load data set
use "Experiments\Experiment02\cbld-model-121020-exp02.dta", clear

* Generate dichotomous consensus variable
generate consensus = 0
replace consensus = 1 if noblock == nostates
label var consensus "Consensus decision"
tab consensus, m


* Identify 'typical' case for illustration in article
*****************************************************

tab consensus if nostates == 25 & vthresh == 72 & sequence == 1, m
* Most decisions are made by consensus
tab nocoal if nostates == 25 & vthresh == 72 & sequence == 1, m
tab nocoal if nostates == 25 & vthresh == 72 & sequence == 1 & consensus == 1, m
* Most cases end with two coalitions negotiating a compromise
tab firstmax if nostates == 25 & vthresh == 72 & sequence == 1, m
tab firstmax if nostates == 25 & vthresh == 72 & sequence == 1 & consensus == 1, m
tab firstmax if nostates == 25 & vthresh == 72 & sequence == 1 & consensus == 1 & nocoal == 2, m
* In most cases, the largest coalition has 8 members
tab step if nostates == 25 & vthresh == 72 & sequence == 1, m
tab step if nostates == 25 & vthresh == 72 & sequence == 1 & consensus == 1 & nocoal == 2 & firstmax == 13, m
* In most cases, the coalition-building process lasts three time-steps
sum meandist if nostates == 25 & vthresh == 72 & sequence == 1, d
sum meandist if nostates == 25 & vthresh == 72 & sequence == 1 & consensus == 1 & nocoal == 2 & firstmax == 13 & step == 3, d
* Identify typical cases
list rseed nostates vthresh if nostates == 25 & vthresh == 72 & sequence == 1 & consensus == 1 /*
	*/ & nocoal == 2 & firstmax == 13 & step == 3 & meandist == r(p50)
* Case with random seed 12032065 is illustrated in the article


* Generate data sets of predicted consensus rates and consensus rates expected by chance
****************************************************************************************

* Collapse data by combination of move sequence, membership size, and voting threshold
sort nostates vthresh
collapse (mean) consensus=consensus, by(sequence nostates vthresh)
label var consensus "Expected consensus (model)"

* Generate standard error of proportion
generate se = sqrt(consensus*(1-consensus)/1000)
label var se "Standard error (model)"

* Generate lower and upper bound of confidence interval
generate lowerci = (consensus - (1.96 * se))
label var lowerci "CI lower bound (model)"
generate upperci = (consensus + (1.96 * se))
label var upperci "CI upper bound (model)"
	
* Generate rule-member state period variable
generate rulemembersize = .
replace rulemembersize = 1 if nostates == 12 & vthresh == 72
replace rulemembersize = 2 if nostates == 15 & vthresh == 72
replace rulemembersize = 3 if nostates == 25 & vthresh == 72
replace rulemembersize = 4 if nostates == 27 & vthresh == 74
replace rulemembersize = 5 if nostates == 27 & vthresh == 65
label var rulemembersize "Rule-membership period"
label define rulemembersizel /*
	*/ 1 "Pre-Nice 12" /*
	*/ 2 "Pre-Nice 15" /*
	*/ 3 "Nice 25" /*
	*/ 4 "Nice 27" /*
	*/ 5 "Lisbon 27", modify
label val rulemembersize rulemembersizel

* Merge with expected consensus rates data
merge m:1 nostates using "Experiments\Other data\cbld-management02.dta"
drop _merge

* Drop redundant observations
drop if rulemembersize == .

* Generate dataset based on model with random execution
sort sequence nostates vthresh
preserve
	* Drop redundant data points
	keep if sequence == 1

	* Save data set
	sort rulemembersize
	save "Experiments\Experiment02\cbld-management03-exp02a.dta", replace
restore

* Generate dataset based on model with ordered execution
preserve
	* Drop redundant data points
	keep if sequence == 2

	* Save data set
	sort rulemembersize
	save "Experiments\Experiment02\cbld-management03-exp02b.dta", replace
restore

* Save entire data set
sort sequence rulemembersize
compress
save "Experiments\Experiment02\cbld-management03-exp02.dta", replace
	
	
* Exit do-file	
log close
exit
